<?php
declare(strict_types=1);
namespace App\Service\Community\Users;
use App\Models\User;
use App\Models\Community\Status;
use Auth;
class UsersShow implements UsersInterface {

    public function user_statuses($user):object{
        return $user->statuses()
            ->orderBy('created_at', 'desc')
            ->paginate(10);
    }
    public function user_index(int $num=6):object
    {
        return User::paginate($num);
    }
    public function feed(int $num=10)
    {
        $feed_items=[];
        if (Auth::check()) {
            $user_ids =Auth::user()->followings->pluck('id')->toArray();
            array_push($user_ids, Auth::user()->id);

            $feed_items=Status::whereIn('user_id', $user_ids)
                ->with('user')
                ->orderBy('created_at', 'desc')->paginate($num);
        }
        return  $feed_items;
    }
    public function follower($user,$type=1,$num=10):array{
        $users=[];
        $title='';
        if ($type==1) {
            $users = $user->followings()->paginate($num);
            $title = $user->name . '关注的人';
        }
        if ($type==2) {
            $users = $user->followers()->paginate($num);
            $title = $user->name . '的粉丝';
        }
        return ['user'=>$users,'title'=>$title];
    }
}
